package com.ssy.lingxi.logistics.serviceimpl;

import cn.hutool.extra.pinyin.PinyinUtil;
import com.ssy.lingxi.common.model.dto.UserLoginCacheDTO;
import com.ssy.lingxi.common.response.ResponseCode;
import com.ssy.lingxi.common.response.Wrapper;
import com.ssy.lingxi.logistics.api.enums.CooperateTypeEnum;
import com.ssy.lingxi.logistics.entity.Company;
import com.ssy.lingxi.logistics.model.enums.CommonStatusEnum;
import com.ssy.lingxi.logistics.model.request.MobileCompanyAddRequest;
import com.ssy.lingxi.logistics.model.response.mobile.MobileCompanyListResponse;
import com.ssy.lingxi.logistics.repository.CompanyRepository;
import com.ssy.lingxi.logistics.service.IMobileCompanyService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 *
 * @author wyx
 * @version 2.0.0
 * @date 2021-12-15
 */
@Service
public class MobileCompanyServiceImpl implements IMobileCompanyService {
    @Resource
    private CompanyRepository companyRepository;


    /**
     * 物流公司列表
     * @param loginUser 登录用户信息
     * @return 返回结果
     */
    @Override
    public Wrapper<List<MobileCompanyListResponse>> listCompany(UserLoginCacheDTO loginUser) {
        List<MobileCompanyListResponse> companyList = companyRepository.findAllByMemberIdAndRoleIdAndCooperateTypeAndStatus(loginUser.getMemberId(), loginUser.getMemberRoleId(), CooperateTypeEnum.MERCHANT.getCode(), CommonStatusEnum.ENABLE.getCode())
                .stream().map(company -> {
                    MobileCompanyListResponse response = new MobileCompanyListResponse();
                    response.setCode(company.getCode());
                    response.setName(company.getName());
                    response.setSort(company.getSort());
                    return response;
                }).collect(Collectors.toList());
        return Wrapper.success(companyList);
    }

    /**
     * 物流公司添加
     * @param request 接口参数
     * @param loginUser 登录用户信息
     * @return 返回结果
     */
    @Override
    public Wrapper<Void> addCompany(MobileCompanyAddRequest request, UserLoginCacheDTO loginUser) {
        // 重复校验
        boolean exists = companyRepository.existsByMemberIdAndRoleIdAndCooperateTypeAndName(loginUser.getMemberId(), loginUser.getMemberRoleId(), CooperateTypeEnum.MERCHANT.getCode(), request.getName());
        if (exists) {
            return Wrapper.fail(ResponseCode.LS_DATA_NO_REPEAT);
        }

        //字符串转换大写拼音
        String upperCaseLetter = PinyinUtil.getPinyin(request.getName(), "").toUpperCase();
        //获取首字母
        String firstLetter = upperCaseLetter.substring(0, 1);

        Company company = new Company();
        company.setCooperateType(CooperateTypeEnum.MERCHANT.getCode());
        company.setStatus(CommonStatusEnum.ENABLE.getCode());
        company.setName(request.getName());
        company.setCode(upperCaseLetter);
        company.setRemark("");
        company.setRoleId(loginUser.getMemberRoleId());
        company.setMemberId(loginUser.getMemberId());
        company.setSort(firstLetter);
        company.setCreateTime(System.currentTimeMillis());

        companyRepository.save(company);
        return Wrapper.success();
    }
}
